package com.house.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @author: GGP
 * @description: jwt工具类
 * @time: 2023-03-19 12:43
 **/
public class JWTUtils {

	private static String SIGN = "token!Q@W3e4r";
	/**
	 * 生成token
	 * @param map
	 * @return 返回token
	 */
	public static String getToken(Map<String,String> map){
		JWTCreator.Builder builder = JWT.create();
		map.forEach(builder::withClaim);
		Calendar instance = Calendar.getInstance();
		instance.add(Calendar.DATE,7);
		builder.withExpiresAt(instance.getTime());
		return builder.sign(Algorithm.HMAC256(SIGN));
	}

	/**
	 * 验证token
	 * @param token
	 */
	public static void verify(String token){
		JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
	}

	///**
	// * 设置过期时间
	// * @param username
	// * @param secret
	// * @return
	// */
	//public static String sign(String username, String secret) {
	//	Date date = new Date(System.currentTimeMillis() + 60 * 30);
	//	Algorithm algorithm = Algorithm.HMAC256(secret);
	//	// 附带username信息
	//	return JWT.create().withClaim("username", username).withExpiresAt(date).sign(algorithm);
	//}

}
